package net.rrkk.photoshow.service;

import net.rrkk.photoshow.entity.Photo;
import net.rrkk.photoshow.repository.PhotoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class PhotoServiceImpl implements PhotoService {
    
    @Autowired
    private PhotoRepository photoRepository;
    
    @Override
    public Photo createPhoto(Photo photo) {
        photo.setCreatedAt(LocalDateTime.now());
        photo.setUpdatedAt(LocalDateTime.now());
        return photoRepository.save(photo);
    }
    
    @Override
    @Cacheable(value = "photos", key = "#id")
    public Optional<Photo> getPhotoById(Long id) {
        return photoRepository.findById(id);
    }
    
    @Override
    @Cacheable(value = "photos", key = "'all'")
    public List<Photo> getAllPhotos() {
        return photoRepository.findAll();
    }
    
    @Override
    @Cacheable(value = "photos", key = "'category_' + #categoryId")
    public List<Photo> getPhotosByCategoryId(Long categoryId) {
        return photoRepository.findByCategoryId(categoryId);
    }
    
    @Override
    @Cacheable(value = "photos", key = "'title_' + #title")
    public List<Photo> searchPhotosByTitle(String title) {
        return photoRepository.findByTitleContaining(title);
    }
    
    @Override
    @Cacheable(value = "photos", key = "'extension_' + #fileExtension")
    public List<Photo> getPhotosByFileExtension(String fileExtension) {
        return photoRepository.findByFileExtension(fileExtension);
    }
    
    @Override
    @Cacheable(value = "photos", key = "'public'")
    public List<Photo> getPublicPhotos() {
        return photoRepository.findByIsPublicTrue();
    }
    
    @Override
    @Cacheable(value = "photos", key = "'private'")
    public List<Photo> getPrivatePhotos() {
        return photoRepository.findByIsPublicFalse();
    }
    
    @Override
    @CacheEvict(value = "photos", key = "#id")
    public Photo updatePhoto(Long id, Photo photoDetails) {
        Optional<Photo> optionalPhoto = photoRepository.findById(id);
        if (optionalPhoto.isPresent()) {
            Photo photo = optionalPhoto.get();
            photo.setCategoryId(photoDetails.getCategoryId());
            photo.setTitle(photoDetails.getTitle());
            photo.setDescription(photoDetails.getDescription());
            photo.setFilePath(photoDetails.getFilePath());
            photo.setFileName(photoDetails.getFileName());
            photo.setFileExtension(photoDetails.getFileExtension());
            photo.setFileSize(photoDetails.getFileSize());
            photo.setWidth(photoDetails.getWidth());
            photo.setHeight(photoDetails.getHeight());
            photo.setMimeType(photoDetails.getMimeType());
            photo.setTags(photoDetails.getTags());
            photo.setIsPublic(photoDetails.getIsPublic());
            photo.setUploadIp(photoDetails.getUploadIp());
            photo.setUpdatedAt(LocalDateTime.now());
            
            return photoRepository.save(photo);
        }
        return null;
    }
    
    @Override
    @CacheEvict(value = "photos", key = "#id")
    public void deletePhoto(Long id) {
        photoRepository.deleteById(id);
    }
    
    @Override
    public void enableCache() {
        // 缓存已通过注解启用
    }
    
    @Override
    public void disableCache() {
        // 在这个实现中，我们不提供禁用缓存的功能
    }
    
    @Override
    public void clearCache() {
        // 缓存清除通过@CacheEvict注解自动处理
    }
}